import { Config } from "./config.js";
var QQMapWX = require('./qqmap-wx-jssdk.min.js');
class Location {
  constructor() {
  }

  // 获取用户授权
  getLocation(param){
    this._setting({
      success:function(){
        // 实例化API核心类
        var qqmapsdk = new QQMapWX({
          key: Config.qqmapsdkKey
        });
        qqmapsdk.reverseGeocoder({
          success: function (res) {
            param.success && param.success(res.result)
          }
        })

        // wx.getLocation({
        //   success: function (res) {
        //     callback&&callback(res)
        //   },
        // })
      },
      fail: function () {
        console.log('用户拒绝') 
        param.fail&&param.fail()
      }  
    })
    
  }

  // 设置用户信息
  _setting (param) {
    var that = this;
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userLocation']) {
          param.success && param.success(res)
        }else{
          wx.authorize({
            scope:"scope.userLocation",
            success:function(res){
              param.success && param.success(res)
            },
            fail:function(){
              param.fail && param.fail(res)
            }
          })
        }
      }
    })
  }
}
export { Location };